import { get, post } from '@/assets/utils/request'
import type {
    LoginData,
    CheckUserRes,
    LoginRes,
    RegisterForm,
    RegisterRes,
    UserInfor,
    UserInforRes,
    UploadAvatarRes
} from './types'

import Mock from 'mockjs'

// 登录
export function login(data: LoginData) {
    return post<LoginRes>('user/login', data)
}

// 检测用户名是否已经存在
export function checkUser(name: string) {
    const params = { name: name }
    return get<CheckUserRes>('user/checkUser', { params: params })
}

// 注册
export function register(data: RegisterForm) {
    return post<RegisterRes>('user/register', data)
}

// 获取登录用户信息
export function getMyInfo() {
    return get<UserInforRes>('user/my')
}

// 获取用户信息
export function getUserInfo(id: string) {
    const params = { id: id }
    return get<UserInforRes>('user/getUserInfo', { params: params })
}

// 上传头像
export function uploadAvatar(file: FormData) {
    return post<UploadAvatarRes>('user/upload_avatar',  file, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}

// 更新用户信息
export function updateUserInfo<T extends UserInfor>(data: T) {
    return post<UserInforRes>('user/infoUpdate', data)
}

// 关注用户
export function following(data: any) {
    return post('user/following', data)
}


const followingsMock = Mock.mock({
    'data|20': [
        {
            'id': '@id',
            'avatarUrl': '',
            'nickname': '@cname',
            'brief': '@csentence(3, 50)'
        }
    ]
})

followingsMock.data.forEach(item => {
    item.avatarUrl = Mock.Random.dataImage('160x160', Mock.Random.title(1, 2))
})

console.log(followingsMock.data)

// 获取关注列表
export function getFollowings(id: string) {
    // return get('user/getFollowings', { params: {id: id} })

    return {
        code: 200,
        list: followingsMock.data
    }
}
